package com.yanqu.road.dao.impl.activity.peakseacraft;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCrossRecordData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 */
public class PeakSeacraftCrossRecordDataDaoImpl extends TempDataObjectDao<PeakSeacraftCrossRecordData> {

    public static String getPeakSeacraftCrossRecordDataSql = "select * from `t_c_activity_peak_seacraft_record` where `activityId`=? and `time`>? and unionGroup=?";
    public List<PeakSeacraftCrossRecordData> getPeakSeacraftCrossRecordDataList(int activityId, long time, int unionGroup) {
        List<PeakSeacraftCrossRecordData> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getPeakSeacraftCrossRecordDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, time);
            call.setLong(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updatePeakSeacraftCrossRecordDataSql = "update `t_c_activity_peak_seacraft_record` set sonType=?,userId=?,targetId=?,killNum=?,score=?,durable=?,goodsUseNum=?,time=?,propId=?,speed=?,continueTime=? where activityId=? and unionGroup=? and unionId=? and masterType=? and area=? and groupNum=?";
    @Override
    protected boolean updateData(PeakSeacraftCrossRecordData peakSeacraftCrossRecordData) {
        peakSeacraftCrossRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getSonType()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossRecordData.getTargetId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getKillNum()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getGoodsUseNum()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossRecordData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getPropId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getSpeed()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getContinueTime()));

        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossRecordData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getMasterType()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getArea()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getGroupNum()));
        if (getDb().executeNoneQuery(updatePeakSeacraftCrossRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPeakSeacraftCrossRecordDataSql = "insert into `t_c_activity_peak_seacraft_record`(activityId,unionGroup,unionId,masterType,sonType,userId,targetId,killNum,score,durable,goodsUseNum,time,propId,speed,continueTime,area,groupNum) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PeakSeacraftCrossRecordData peakSeacraftCrossRecordData) {
        peakSeacraftCrossRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossRecordData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getMasterType()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getSonType()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossRecordData.getTargetId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getKillNum()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getGoodsUseNum()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossRecordData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getPropId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getSpeed()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getContinueTime()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getArea()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossRecordData.getGroupNum()));
        if (getDb().executeNoneQuery(insertPeakSeacraftCrossRecordDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PeakSeacraftCrossRecordData parseData(ResultSet rs) throws SQLException {
        PeakSeacraftCrossRecordData peakSeacraftCrossRecordData = new PeakSeacraftCrossRecordData();
        peakSeacraftCrossRecordData.setActivityId(rs.getInt("activityId"));
        peakSeacraftCrossRecordData.setUnionGroup(rs.getInt("unionGroup"));
        peakSeacraftCrossRecordData.setUnionId(rs.getString("unionId"));
        peakSeacraftCrossRecordData.setMasterType(rs.getInt("masterType"));
        peakSeacraftCrossRecordData.setSonType(rs.getInt("sonType"));
        peakSeacraftCrossRecordData.setUserId(rs.getLong("userId"));
        peakSeacraftCrossRecordData.setTargetId(rs.getString("targetId"));
        peakSeacraftCrossRecordData.setKillNum(rs.getInt("killNum"));
        peakSeacraftCrossRecordData.setScore(rs.getInt("score"));
        peakSeacraftCrossRecordData.setDurable(rs.getInt("durable"));
        peakSeacraftCrossRecordData.setGoodsUseNum(rs.getInt("goodsUseNum"));
        peakSeacraftCrossRecordData.setTime(rs.getLong("time"));
        peakSeacraftCrossRecordData.setPropId(rs.getInt("propId"));
        peakSeacraftCrossRecordData.setSpeed(rs.getInt("speed"));
        peakSeacraftCrossRecordData.setContinueTime(rs.getInt("continueTime"));
        peakSeacraftCrossRecordData.setArea(rs.getInt("area"));
        peakSeacraftCrossRecordData.setGroupNum(rs.getInt("groupNum"));

        return peakSeacraftCrossRecordData;
    }
}
